import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
import React, { useState, useMemo } from 'react';
import { connect } from 'dva';
import { Spin } from 'antd';
import { getEnvConfig } from "../../utils/iocUtils";
import { getAccessToken } from "../../utils/utils";
import styles from "./index.less?modules";

function Link(props) {
  var _props$match = props.match;
  _props$match = _props$match === void 0 ? {} : _props$match;
  var _props$match$params = _props$match.params;
  _props$match$params = _props$match$params === void 0 ? {} : _props$match$params;
  var _props$match$params$l = _props$match$params.link,
      link = _props$match$params$l === void 0 ? '' : _props$match$params$l,
      _props$global = props.global;
  _props$global = _props$global === void 0 ? {} : _props$global;
  var _props$global$menuLea = _props$global.menuLeafNode,
      menuLeafNode = _props$global$menuLea === void 0 ? [] : _props$global$menuLea;

  var _useState = useState(true),
      _useState2 = _slicedToArray(_useState, 2),
      initLoading = _useState2[0],
      setInitLoading = _useState2[1];

  var _useState3 = useState(getAccessToken()),
      _useState4 = _slicedToArray(_useState3, 1),
      accessToken = _useState4[0];

  var API_HOST = useMemo(function () {
    return getEnvConfig().API_HOST;
  }, []);
  var menu = menuLeafNode.find(function (item) {
    return item.id.toString() === link;
  }) || {};
  var type = menu.type || '';
  var url = '';

  if (type === 'link') {
    url = menu.path.startsWith('http') ? menu.path : "http://".concat(menu.path);
  } else if (type === 'inner-link') {
    if (menu.path.startsWith('http')) {
      url = menu.path;
    } else {
      url = "".concat(API_HOST).concat(menu.path);
    }
  }

  if (type === 'inner-link') {
    url = "".concat(url).concat(url.includes('?') ? '&' : '?', "access_token=").concat(accessToken, "&from=cpaas");
  }

  return (type === 'link' || type === 'inner-link') && /*#__PURE__*/React.createElement(Spin, {
    spinning: initLoading,
    wrapperClassName: styles['iframe-loading']
  }, /*#__PURE__*/React.createElement("iframe", {
    onLoad: function onLoad() {
      return setInitLoading(false);
    },
    title: link,
    src: url,
    style: {
      width: '100%',
      height: '100%',
      verticalAlign: 'top'
    },
    frameBorder: "0"
  }));
}

export default connect(function (_ref) {
  var global = _ref.global;
  return {
    global: global
  };
})(Link);